*"* use this source file for the definition and implementation of
*"* local helper classes, interface definitions and type
*"* declarations
 CLASS lcl_fs_interface DEFINITION.

   PUBLIC SECTION.
* Attributes
     DATA:iv_json TYPE string,
          ov_json TYPE string,
          lv_method TYPE string,
          lv_json TYPE string.

     DATA lo_http_client TYPE REF TO if_web_http_client.
     DATA lv_path TYPE string.
* Methods
     METHODS get_token
       IMPORTING
                 ls_appid     TYPE t_appid
       RETURNING VALUE(token) TYPE string.

     METHODS get_tasks
       IMPORTING
                 iv_token         TYPE string
                 iv_approval_code TYPE string
*       RETURNING VALUE(e_msg)     TYPE string.
       RETURNING VALUE(e_msg)     TYPE tt_ins.

     METHODS get_instance
       IMPORTING
                 iv_token     TYPE string
                 iv_instance  TYPE string
*       RETURNING VALUE(e_msg) TYPE string.
       RETURNING VALUE(e_msg) TYPE tt_form.

     METHODS set_interface_log
       IMPORTING
         comm_scenario  TYPE if_com_management=>ty_cscn_id " string
         service_id     TYPE if_com_management=>ty_cscn_outb_srv_id " string
         comm_system_id TYPE if_com_management=>ty_cscn_id " string
         iv_method      type string
         iv_json        TYPE string
         io_json        TYPE string.

   PROTECTED SECTION.
   PRIVATE SECTION.
     CONSTANTS zfeishu_cs TYPE if_com_management=>ty_cscn_id VALUE 'ZFEISHU_CS' ##NO_TEXT.
     CONSTANTS zfeishu_out_01_rest TYPE if_com_management=>ty_cscn_outb_srv_id VALUE 'ZFEISHU_OUT_001_REST' ##NO_TEXT.
     CONSTANTS zfeishu_out_02_rest TYPE if_com_management=>ty_cscn_outb_srv_id VALUE 'ZFEISHU_OUT_002_REST' ##NO_TEXT.
     CONSTANTS zfeishu_out_03_rest TYPE if_com_management=>ty_cscn_outb_srv_id VALUE 'ZFEISHU_OUT_003_REST' ##NO_TEXT.
 ENDCLASS.

 CLASS lcl_fs_interface IMPLEMENTATION.

   METHOD get_token.

     CLEAR: iv_json,ov_json.
     TRY.
         DATA(lo_url_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
         "Should be your communication scenario
           comm_scenario = zfeishu_cs
           service_id = zfeishu_out_01_rest
           comm_system_id = CONV #( zfeishu_cs ) ).

         DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_url_destination ).
         DATA(req) = http_client->get_http_request(  ).

         req->set_header_field( i_name = 'Content-type' i_value = 'application/json; charset=UTF-8' ).
         iv_json = /ui2/cl_json=>serialize( data = ls_appid ).
         req->set_text(  iv_json ).

         DATA(lo_response) = http_client->execute( if_web_http_client=>post ).
         ov_json = lo_response->get_text( ).
         DATA ls_return TYPE  t_return.

         CALL METHOD /ui2/cl_json=>deserialize
           EXPORTING
             json = ov_json
           CHANGING
             data = ls_return.

         IF ls_return-code = 0.
           RETURN ls_return-tenant_access_token.
         ENDIF.

       CATCH cx_root INTO DATA(e).
         DATA(error) = e->get_longtext( ).
     ENDTRY.

     CLEAR lv_method .
     lv_method = 'POST'.
     me->set_interface_log( comm_scenario = zfeishu_cs
                            service_id = zfeishu_out_01_rest
                            comm_system_id = zfeishu_cs
                            iv_method = lv_method
                            iv_json = iv_json
                            io_json = ov_json
                            ).
   ENDMETHOD.

   METHOD get_tasks.
     CLEAR: iv_json,ov_json.
     TRY.
         DATA(lo_url_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
          "Should be your communication scenario
           comm_scenario = zfeishu_cs
           service_id = zfeishu_out_02_rest
           comm_system_id = CONV #( zfeishu_cs ) ).

         DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_url_destination ).
         DATA(req) = http_client->get_http_request(  ).

         req->set_header_field( i_name = 'Content-type' i_value = 'application/json; charset=UTF-8' ).
         req->set_form_field( i_name = 'page_size' i_value = '10').
         req->set_authorization_bearer( iv_token ).
         DATA ls_task TYPE t_task.

         ls_task-approval_code = iv_approval_code.
         ls_task-locale = |zh-CN|.
         ls_task-order = |2|.
         ls_task-task_start_time_from = |1761965716|.
         ls_task-task_start_time_to = |1764557716|.
         ls_task-task_status = |ALL|.

******************下面大小写转换
         TYPES: BEGIN OF name_mapping,
                  abap TYPE abap_compname,
                  json TYPE string,
                END OF name_mapping.

         DATA: lw_name_mapping TYPE name_mapping,
               name_mappings   TYPE HASHED TABLE OF name_mapping WITH UNIQUE KEY abap.
         DATA: lt_name_mappings    LIKE name_mappings.

         " 大小写mapping，
         lt_name_mappings = VALUE #(
         ( abap = 'APPROVAL_CODE'        json = 'approval_code' )
         ( abap = 'LOCALE' json = 'locale' )
         ( abap = 'ORDER' json = 'order' )
         ( abap = 'TASK_START_TIME_FROM' json = 'task_start_time_from' )
         ( abap = 'TASK_START_TIME_TO' json = 'task_start_time_to' )
         ( abap = 'TASK_STATUS' json = 'task_status' )
         ).

         iv_json = /ui2/cl_json=>serialize(
         data = ls_task
         pretty_name   = /ui2/cl_json=>pretty_mode-low_case " 大写转小写
          name_mappings = lt_name_mappings[]
          ).
         req->set_text(  iv_json ).

         DATA(lo_response) = http_client->execute( if_web_http_client=>post ).
         ov_json = lo_response->get_text( ).
         " TODO 定义结构输出
         DATA:ls_return1 TYPE t_return1.
         CALL METHOD /ui2/cl_json=>deserialize
           EXPORTING
             json = ov_json
           CHANGING
             data = ls_return1.

         DATA ls_data TYPE t_data.
         ls_data = ls_return1-data.

         DATA:lt_task_l TYPE TABLE OF t_task_l.
         MOVE-CORRESPONDING ls_data-task_list TO lt_task_l.

         DATA:lt_ins TYPE TABLE OF t_ins,
              ls_ins TYPE t_ins.

         LOOP AT lt_task_l INTO DATA(ls_task_l).
           ls_ins = ls_task_l-instance.
           APPEND ls_ins TO lt_ins.
           CLEAR:ls_ins,ls_task_l.
         ENDLOOP.

         IF lt_ins IS NOT INITIAL.
           SORT lt_ins BY code status.
           DELETE ADJACENT DUPLICATES FROM lt_ins COMPARING code status.
           RETURN lt_ins.
         ENDIF.

       CATCH cx_root INTO DATA(e).
         DATA(error) = e->get_longtext( ).
     ENDTRY.

     CLEAR lv_method .
     lv_method = 'POST'.
     me->set_interface_log( comm_scenario = zfeishu_cs
                                service_id = zfeishu_out_01_rest
                                comm_system_id = zfeishu_cs
                                iv_method = lv_method
                                iv_json = iv_json
                                io_json = ov_json
                                ).
   ENDMETHOD.

   METHOD  get_instance.
     CLEAR: iv_json,ov_json,lv_json.
     TRY.
         DATA(lo_url_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
           comm_scenario = zfeishu_cs
           service_id = zfeishu_out_03_rest
           comm_system_id = CONV #( zfeishu_cs ) ).

         DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_url_destination ).
         DATA(req) = http_client->get_http_request(  ).
         req->set_header_field( i_name = 'Content-type' i_value = 'application/json; charset=UTF-8' ).
         CONCATENATE '/' iv_instance INTO lv_path.
         req->set_uri_path( |{ lv_path }| ).
         req->set_authorization_bearer( iv_token ).
         req->set_text(  iv_json ).

         DATA(lo_response) = http_client->execute( if_web_http_client=>get ).
         ov_json = lo_response->get_text( ).

***********  获取的单个实例审批输出结构****************
         DATA:ls_return2 TYPE t_return2.
         lv_json = ov_json.
         REPLACE ALL OCCURRENCES OF '\"' IN lv_json WITH '"' .
         REPLACE ALL OCCURRENCES OF '\\"' IN lv_json WITH '"' .
         REPLACE ALL OCCURRENCES OF '"[{' IN lv_json WITH '[{' .
         REPLACE ALL OCCURRENCES OF '}]"' IN lv_json WITH '}]' .

         CALL METHOD /ui2/cl_json=>deserialize
           EXPORTING
             json = lv_json
           CHANGING
             data = ls_return2.

         DATA ls_data2 TYPE t_data2.
         ls_data2 = ls_return2-data.

         DATA:lt_form TYPE TABLE OF t_form.
         MOVE-CORRESPONDING ls_data2-form TO lt_form.

         IF lt_form IS NOT INITIAL.
           RETURN lt_form.
         ENDIF.

       CATCH cx_root INTO DATA(e).
         DATA(error) = e->get_longtext( ).
     ENDTRY.
     CLEAR lv_method .
     lv_method = 'GET'.

     me->set_interface_log( comm_scenario = zfeishu_cs
                                service_id = zfeishu_out_03_rest
                                comm_system_id = zfeishu_cs
                                iv_method = lv_method
                                iv_json = iv_json
                                io_json = ov_json
                                ).
   ENDMETHOD.

   METHOD set_interface_log.

    DATA: cs_ztiflog TYPE ztiflog.
    DATA abap_context_info TYPE REF TO cl_abap_context_info.
    abap_context_info = NEW #( ).
    GET TIME STAMP FIELD DATA(lv_timestamp).

    TRY.
        cs_ztiflog-log_uuid = cl_system_uuid=>create_uuid_x16_static( ).
      CATCH cx_uuid_error.
        "handle exception
    ENDTRY.
    cs_ztiflog-comm_scenario = comm_scenario .
    cs_ztiflog-comm_system_id = comm_system_id.
    cs_ztiflog-function = service_id.
    cs_ztiflog-zclass =  SY-repid.
    cs_ztiflog-sdate = abap_context_info->get_system_date( ).
    cs_ztiflog-stime = abap_context_info->get_system_time( ).
    cs_ztiflog-method = iv_method.
    cs_ztiflog-json_i = iv_json.
    cs_ztiflog-json_O = io_json.
    cs_ztiflog-direction = 'OUTBOUND'.
    cs_ztiflog-json_length_i = strlen( cs_ztiflog-json_i ) .
    cs_ztiflog-json_length_o = strlen( cs_ztiflog-json_o ) .
    cs_ztiflog-timestamp_i = lv_timestamp.
    cs_ztiflog-usr = abap_context_info->get_user_technical_name( ).
    cs_ztiflog-usr_name = abap_context_info->get_user_alias( ).

    IF cs_ztiflog IS NOT INITIAL.
      MODIFY ztiflog FROM @cs_ztiflog.
      COMMIT WORK.
    ENDIF.

   ENDMETHOD.

 ENDCLASS.
